package com.gelpag.base.business.unit;

import been.RoleInfo;
import been.UserInfo;
import com.gelpag.base.business.bean.PageBean;
import com.gelpag.base.business.common.CommonBase;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import entity.Const;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tools.utils.Common;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/unit")
public class UnitController {

    @Autowired
    UnitService unitService;

    @PostMapping("/getUnitData")
    public Result getUnitData(HttpServletRequest request) {
        String token = request.getHeader("token");

        try {
            List<UnitInfo> unitInfoList = unitService.getUnitData();
            return new Result(Const.RESULT_0, Const.ERROR_MSG_0, token,unitInfoList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Const.RESULT_1, Const.ERROR_MSG_1, token,"");
        }
    }

    @PostMapping("/saveUnit")
    public Result saveUnit(HttpServletRequest request, UnitInfo unitInfo) {

        String token = request.getHeader("token");

        try {
            //参数验证
            Map<String, String> checkMap = isPassSaveUnit(unitInfo);
            if (Const.NOT_NOMAL_PARAM.equals(checkMap.get(Const.PARAM_KEY))) {
                return new Result(Const.RESULT_1, checkMap.get(Const.PARAM_RET_KEY), token, "");
            }
            UserInfo userInfo = CommonBase.getUserInFromToken(token);

            if (userInfo == null) {
                return new Result(Const.RESULT_F1, Const.ERROR_MSG_F1, token,"");
            }
            unitService.saveUnitInfo(unitInfo, userInfo);
            return new Result(Const.RESULT_0, Const.ERROR_MSG_0,token,"");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Const.RESULT_1, Const.ERROR_MSG_1,token,"");
        }
    }

    private Map<String, String> isPassSaveUnit(UnitInfo unitInfo) {
        Map<String, String> checkMap = new HashMap<>();
        //默认返回正常
        checkMap.put(Const.PARAM_KEY, Const.NOMAL_PARAM);
        if (Common.isNullString(unitInfo.getUnit_name())) {
            checkMap.put(Const.PARAM_KEY, Const.NOT_NOMAL_PARAM);
            checkMap.put(Const.PARAM_RET_KEY, "部门名称不能为空！");
            return checkMap;
        }
        if (Common.isNullString(unitInfo.getParent_unit_id())) {
            checkMap.put(Const.PARAM_KEY, Const.NOT_NOMAL_PARAM);
            checkMap.put(Const.PARAM_RET_KEY, "上级部门不能为空！");
            return checkMap;
        }
        return checkMap;
    }

    @PostMapping("/getListData")
    public Result getListData(HttpServletRequest request, UnitInfo unitInfo) {

        String token = request.getHeader("token");
        Map<String, String> checkMap = isPassGetListData(unitInfo.getPageNum(), unitInfo.getPageSize(), unitInfo.getUnit_id());
        if (Const.NOT_NOMAL_PARAM.equals(checkMap.get(Const.PARAM_KEY))) {
            return new Result(Const.RESULT_1, checkMap.get(Const.PARAM_RET_KEY),token,"");
        }

        try {
            int pageNum = Common.getPageNum(unitInfo.getPageNum());
            int pageSize = Common.getPageSize(unitInfo.getPageSize());
            Page<UnitInfo> page = PageHelper.startPage(pageNum, pageSize);
            unitService.getListData(unitInfo);
            PageBean<UnitInfo> pageData = new PageBean<>(pageNum, pageSize, page.getTotal(), page.getPages(), page.getResult());
            return new Result(Const.RESULT_0, Const.ERROR_MSG_0,token,pageData);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Const.RESULT_1, Const.ERROR_MSG_1,token, "");
        }
    }

    private Map<String, String> isPassGetListData(String page_num, String page_size, String unit_id) {
        Map<String, String> checkMap = new HashMap<>();
        //默认返回正常
        checkMap.put(Const.PARAM_KEY, Const.NOMAL_PARAM);
        if (Common.isNullString(page_num)) {
            checkMap.put(Const.PARAM_KEY, Const.NOT_NOMAL_PARAM);
            checkMap.put(Const.PARAM_RET_KEY, "page_num不能为空！");
            return checkMap;
        }
        if (Common.isNullString(page_size)) {
            checkMap.put(Const.PARAM_KEY, Const.NOT_NOMAL_PARAM);
            checkMap.put(Const.PARAM_RET_KEY, "page_size不能为空！");
            return checkMap;
        }
        if (Common.isNullString(unit_id)) {
            checkMap.put(Const.PARAM_KEY, Const.NOT_NOMAL_PARAM);
            checkMap.put(Const.PARAM_RET_KEY, "unit_id不能为空！");
            return checkMap;
        }
        return checkMap;
    }

    @PostMapping("/getRoleList")
    public Result getRoleList(HttpServletRequest request) {

        String token = request.getHeader("token");

        try {
            List<RoleInfo> roleList = unitService.getRoleList();
            return new Result(Const.RESULT_0, Const.ERROR_MSG_0,token, roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Const.RESULT_1, Const.ERROR_MSG_1,token, "");
        }
    }

    @PostMapping("/saveUser")
    public Result saveUser(HttpServletRequest request, UnitInfo unitInfo) {

        String token = request.getHeader("token");
        //验证
        Map<String, String> checkMap = isPassSaveUser(unitInfo);
        if (Const.NOT_NOMAL_PARAM.equals(checkMap.get(Const.PARAM_KEY))) {
            return new Result(Const.RESULT_1, checkMap.get(Const.PARAM_RET_KEY),token,"");
        }
        try {
            UserInfo userInfo = CommonBase.getUserInFromToken(token);
            if (userInfo == null) {
                return new Result(Const.RESULT_F1, Const.ERROR_MSG_F1, token, "");
            }
            unitService.saveUser(unitInfo);
            return new Result(Const.RESULT_0, Const.ERROR_MSG_0,token,"");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Const.RESULT_1, Const.ERROR_MSG_1,token,"");
        }
    }

    private Map<String, String> isPassSaveUser(UnitInfo unitInfo) {
        Map<String, String> checkMap = new HashMap<>();
        //默认返回正常
        checkMap.put(Const.PARAM_KEY, Const.NOMAL_PARAM);

        if (Common.isNullString(unitInfo.getUser_name())) {
            checkMap.put(Const.PARAM_KEY, Const.NOT_NOMAL_PARAM);
            checkMap.put(Const.PARAM_RET_KEY, "用户名称不能为空！");
            return checkMap;
        }
        if (Common.isNullString(unitInfo.getLogin_name())) {
            checkMap.put(Const.PARAM_KEY, Const.NOT_NOMAL_PARAM);
            checkMap.put(Const.PARAM_RET_KEY, "登录账号不能为空！");
            return checkMap;
        }
        if (Common.isNullString(unitInfo.getRole_ids())) {
            checkMap.put(Const.PARAM_KEY, Const.NOT_NOMAL_PARAM);
            checkMap.put(Const.PARAM_RET_KEY, "角色ids，不能为空！");
            return checkMap;
        }
        if (Common.isNullString(unitInfo.getUnit_id())) {
            checkMap.put(Const.PARAM_KEY, Const.NOT_NOMAL_PARAM);
            checkMap.put(Const.PARAM_RET_KEY, "unit_id不能为空！");
            return checkMap;
        }
        //验证登陆账号是否存在
        UserInfo userInfo = unitService.getUserInfoByLoginName(unitInfo.getLogin_name());
        if (userInfo!=null && Common.isNullString(unitInfo.getUser_id())) {
            checkMap.put(Const.PARAM_KEY,Const.NOT_NOMAL_PARAM);
            checkMap.put(Const.PARAM_RET_KEY, "账号已存在，请重新输入");
            return checkMap;
        }

        return checkMap;
    }
}
